package com.huawei.spare.part.replace.utils;

import java.io.IOException;
import net.schmizz.keepalive.KeepAliveProvider;
import net.schmizz.sshj.DefaultConfig;
import net.schmizz.sshj.SSHClient;
import net.schmizz.sshj.connection.channel.direct.Session;
import net.schmizz.sshj.transport.verification.PromiscuousVerifier;

public class SSHHelper {

  public static DefaultConfig getKeepAliveConfig() {
    DefaultConfig config = new DefaultConfig();
    config.setKeepAliveProvider(KeepAliveProvider.KEEP_ALIVE);
    return config;
  }

  public static SSHClient newSSHClient(String host, String username, String password) throws IOException {
    final SSHClient ssh = new SSHClient();
    ssh.addHostKeyVerifier(new PromiscuousVerifier());
    ssh.setConnectTimeout(20000);
    ssh.setTimeout(30000);
    ssh.connect(host);
    //ssh.getConnection().getKeepAlive().setKeepAliveInterval(5);
    ssh.authPassword(username, password);
    return ssh;
  }

  public static Session allocateSSHSession(String host, String username, String password) throws IOException {
    final SSHClient ssh = new SSHClient();
    ssh.addHostKeyVerifier(new PromiscuousVerifier());
    ssh.connect(host);
    ssh.authPassword(username, password);
    ssh.isAuthenticated();
    return ssh.startSession();
  }

}
